package src.反射6;


import org.apache.commons.beanutils.BeanUtils;
import src.反射7.MyhandleDbReflection;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class preparedStatement {
    /*Date:2025/3/18
                                 连接数据库的一些方法
 * api:
 *   querybean():  查询表的所有数据  1.sql：返回sql语句，类似select * from stu where ？
 *                          2.反射的可变参数
 *                          3.可变参数
 *                  return T
 *
 *   queryMap（）：获取表中某一条记录，以hashcode形式（键值对）存储返回
 *                         1.sql：返回sql语句，类似select * from stu where ？
 *                         2.可变参数
 *                  return   Map<String,Object>

     queryList（）：把多个hashmap对象封装到list集合中
     *                     1.sql：返回sql语句，类似select * from stu where ？
 *                         2.可变参数
 *                   return ArrayList<Map>

     queryArray(): 获取表中的一条数据，以集合的形式返回
                            1.sql：返回sql语句，类似select * from stu where ？
 *                         2.可变参数
 *                    return Object[]
 *
 *   queryArrayList(): 把多个数组对象以集合的形式返回
 *                         1.sql：返回sql语句，类似select * from stu where ？
 *                         2.可变参数
 *                    return  ArrayList<Object[]>

      close():关闭资源
      *                    1.connection：连接数据库
      *                    2.resultSet：执行sql语句
      *                    3.statement：预编译对象
 */

    public class queryBean {
        public static <T> T querybean(String sql,Class<T>cls,Object...parameters) throws Exception {
            //定义对象
            Connection connection=null;
            //预编译
            PreparedStatement statement=null;
            //原数据
            ResultSet resultSet=null;
            //定义一个t储存数据
            T t=null;
            //连接数据库
            connection = MyhandleDbReflection.getConnection();
            preparedStatement preparedStatement = new preparedStatement();
            statement=MyhandleDbReflection.PreSta(connection,sql,parameters);
            resultSet=statement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            while(resultSet.next()){
                t=cls.getConstructor().newInstance();
                //获取这一行记录字段的字数
                int columnName = metaData.getColumnCount();
                for (int i = 1; i <= columnName; i++) {
                    BeanUtils.setProperty(t,
                            metaData.getColumnName(i),
                            resultSet.getObject(i));
                }
            }
            queryBean.close(connection,resultSet,statement);
            return t;
        }
        /*
    获取表中某一条记录，以hashcode形式（键值对）存储返回
    */
        public static Map<String,Object> queryMap(String sql, Object...parameters) throws Exception {
            Connection connection = MyhandleDbReflection.getConnection();
            PreparedStatement statement =MyhandleDbReflection.PreSta(connection,sql, parameters);
            ResultSet resultSet = statement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            resultSet.next();
            for (int i = 1; i <= columnCount; i++) {
                stringObjectHashMap.put(metaData.getColumnName(i),resultSet.getObject(i));
            }
            queryBean.close(connection,resultSet,statement);
            return stringObjectHashMap;
        }

        /*
        把多个hashmap对象封装到list集合中
         */
        public static ArrayList<Map> queryList(String sql, Object...parameters) throws Exception {
            Connection connection = MyhandleDbReflection.getConnection();
            PreparedStatement statement = MyhandleDbReflection.PreSta(connection, sql, parameters);
            ResultSet resultSet = statement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            //创建一个ArrayList集合存储hashcode的数据
            ArrayList<Map> maps = new ArrayList<>();
            while (resultSet.next()) {
                HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    stringObjectHashMap.put(metaData.getColumnName(i), resultSet.getObject(i));
                }
                maps.add(stringObjectHashMap);
            }
            queryBean.close(connection,resultSet,statement);
            return maps;
        }

        /*
        获取表中的一条数据，以集合的形式返回
         */
        public static Object[] queryArray(String sql,Object...parameters) throws Exception {
            //创建一个数组
            Connection connection = MyhandleDbReflection.getConnection();
            PreparedStatement statement =MyhandleDbReflection.PreSta(connection, sql, parameters);
            ResultSet resultSet = statement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            Object[] objects = new Object[columnCount];
            resultSet.next();
            for (int i = 0; i <columnCount ; i++) {
                objects[i] = resultSet.getObject(i+1);
            }
            queryBean.close(connection,resultSet,statement);
            return objects;
        }
        /*
        把多个数组对象以集合的形式返回
         */
        public  static ArrayList<Object[]> queryArrayList(String sql,Object...parameters) throws Exception {
            Connection connection = MyhandleDbReflection.getConnection();
            PreparedStatement statement =MyhandleDbReflection.PreSta(connection, sql, parameters);
            ResultSet resultSet = statement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            //创建一个集合存储数组对象
            ArrayList<Object[]> objects = new ArrayList<>();
            while(resultSet.next()){
                Object[] objects1 = new Object[columnCount];
                for (int i =1 ; i <= columnCount; i++) {
                    objects1[i] = resultSet.getObject(i+1);
                }
                objects.add(objects1);
            }
            queryBean.close(connection,resultSet,statement);
            return objects;
        }
        public static void close(Connection connection,ResultSet resultSet,PreparedStatement statement) throws SQLException, SQLException, SQLException {
            if (connection!=null){
                connection.close();
            }
            if (resultSet!=null){
                resultSet.close();
            }
            if (statement!=null){
                statement.close();
            }
        }
    }
}
